package org.m2acsi.control;

import java.io.Serializable;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.m2acsi.entities.RoleType;
import org.m2acsi.entities.User;
import org.m2acsi.utils.Security;

@Named
@RequestScoped
@ManagedBean
public class UserBean extends BaseBean implements Serializable {

    private List<User> users;
    private String message;
    private String filtre;
    private String tmpRole;
    private long tmpUserId;
    private User tmpUser = new User();

    public UserBean() {
        super();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTmpRole() {
        return tmpRole;
    }

    public void setTmpRole(String tmpRole) {
        this.tmpRole = tmpRole;
    }

    public long getTmpUserId() {
        return tmpUserId;
    }

    public void setTmpUserId(long tmpUserId) {
        this.tmpUserId = tmpUserId;
    }

    public User getTmpUser() {
        return tmpUser;
    }

    public void setTmpUser(User tmpUser) {
        this.tmpUser = tmpUser;
    }

    public String getFiltre() {
        return filtre;
    }

    public void setFiltre(String filtre) {
        this.filtre = filtre;
    }

    public List<User> getUsers() {
        if (users == null) {
            users = userEjb.findAllUsers(filtre);
        }
        return users;
    }

    public String getUserRole(String email) {
        tmpUser = userEjb.findUser(email);
        return tmpUser == null ? "" : tmpUser.getRoleName().name();
    }

    public User loadUser() {
        tmpUser = userEjb.find(tmpUserId);
        tmpRole = tmpUser.getRoleName().toString();
        return tmpUser;
    }

    public List<User> getUsersAdminRole() {
        users = userEjb.findUsers(RoleType.ADMIN);
        return users;
    }

    public List<User> getUsersUserRole() {
        users = userEjb.findUsers(RoleType.USER);
        return users;
    }

    public List<User> getUsersDisabledRole() {
        users = userEjb.findUsers(RoleType.DISABLED);
        return users;
    }

    public void addUser() {
        switch (tmpRole) {
            case "Admin":
                tmpUser.setRoleName(RoleType.ADMIN);
                break;
            default:
                tmpUser.setRoleName(RoleType.USER);
                break;
        }
        //message is unencrypted password
        message = tmpUser.getPassword();
        tmpUser.setPassword(Security.sha256(tmpUser.getPassword()));
        try {
            userEjb.save(tmpUser);
            redirectWithMessage("/faces/admin/userList.xhtml", tmpUser.getEmail() + " user successfully created");
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage("user-add-form:email", new FacesMessage("This email address is already used."));
            tmpUser.setPassword("");
        }
    }

    public void editUser() {
        User userDB = userEjb.find(tmpUserId);
        userDB.setEmail(tmpUser.getEmail());
        userDB.setFirstname(tmpUser.getFirstname());
        userDB.setLastname(tmpUser.getLastname());
        switch (tmpRole) {
            case "ADMIN":
                userDB.setRoleName(RoleType.ADMIN);
                break;
            case "USER":
                userDB.setRoleName(RoleType.USER);
                break;
            default:
                userDB.setRoleName(RoleType.DISABLED);
                break;
        }
        try {
            tmpUser = userEjb.update(userDB);
            redirectWithMessage("/faces/admin/userList.xhtml", tmpUser.getEmail() + "'s information successfully edited.");
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage("user-edit-form:user-edit-submit", new FacesMessage("Sorry, an error occured. Please double-check the information you provided."));
        }
    }

    public void removeUser(User u) {
        try {
            userEjb.remove(u);
            users = userEjb.findAllUsers(filtre);
            redirectWithMessage("/faces/admin/userList.xhtml", u.getEmail() + " has been disabled.");
        } catch (Exception e) {
            redirectWithMessage("/faces/admin/userList.xhtml", "Sorry, an error occured when trying to remove this user.", 5000);
        }
    }

}
